/* global window */
import modelExtend from 'dva-model-extend'
import { config, arrayToTree } from 'utils'
import { message } from 'antd'
import { create, menuQuerySub, menuQuery, menuQueryAll } from 'services/menu'
import * as usersService from 'services/users'
import { pageModel } from './common'

const { query } = usersService
const { prefix } = config
export default modelExtend(pageModel, {
  namespace: 'menu',

  state: {
    currentItem: {},
    modalVisible: false,
    modalType: 'create',
    selectedRowKeys: [],
  },

  subscriptions: {
    setup ({ dispatch, history }) {
      history.listen((location) => {
        if (location.pathname === '/menu') {
          dispatch({
            type: 'query',
          })
        }
      })
    },
  },

  effects: {

    * query ({ payload= {} }, { call, put }) {
      const data = yield call(menuQueryAll)
      if (data) {
        yield put({
          type: 'querySuccess',
          payload: {
            list: arrayToTree(data.MENU, 'ID', 'PARENT_ID', 'children'),
          },
        })
      }
    },

    * delete ({ payload }, { call, put, select }) {
      const data = yield call(menuQuery, { id: payload })
      const { selectedRowKeys } = yield select(_ => _.user)
      if (data.success) {
        message.success('delete success')
        yield put({ type: 'updateState', payload: { selectedRowKeys: selectedRowKeys.filter(_ => _ !== payload) } })
        yield put({ type: 'query' })
      } else {
        message.error('delete failed')
        throw data
      }
    },

    * multiDelete ({ payload }, { call, put }) {
      const data = yield call(usersService.remove, payload)
      if (data.success) {
        message.success('delete success')
        yield put({ type: 'updateState', payload: { selectedRowKeys: [] } })
        yield put({ type: 'query' })
      } else {
        message.error('delete failed')
        throw data
      }
    },

    * create ({ payload }, { call, put }) {
      const data = yield call(create, payload)
      if (data.success) {
        message.success('create success')
        yield put({ type: 'hideModal' })
        yield put({ type: 'query' })
      } else {
        message.error('create failed')
        throw data
      }
    },

    * update ({ payload }, { select, call, put }) {
      const id = yield select(({ user }) => user.currentItem.ID)
      const newUser = { ...payload, id }
      const data = yield call(menuQuery, newUser)
      if (data.success) {
        message.success('update success')
        yield put({ type: 'hideModal' })
        yield put({ type: 'query' })
      } else {
        message.error('update failed')
        throw data
      }
    },

    * showModal ({ payload }, { call, put }) {
      let sendData = {}
      sendData.ID = payload.currentItem.ID
      const data = yield call(menuQuery, sendData)
      if (data.success) {
        yield put({
          type: 'updateModalState',
          payload: {
            modalType: 'update',
            currentItem: data,
            modalVisible: true,
          },
        })
      } else {
        throw data
      }
    },
  },

  reducers: {
    updateSelectState (state, { payload }) {
      return { ...state, ...payload }
    },

    updateModalState (state, { payload }) {
      return { ...state, ...payload, modalVisible: true }
    },

    hideModal (state) {
      return { ...state, modalVisible: false }
    },
  },
})
